package com.bookshop.controller;

import com.bookshop.model.CatalogModel;
import com.bookshop.service.CartService;
import com.bookshop.service.CatalogService;
import com.bookshop.vo.CartVo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

    @Controller
    public class ShoppingController {
        @Autowired
        private CartService cartService;
        @Autowired
        private CatalogService catalogService;
        //    查询购物车
    @RequestMapping("/shoplist")
    public String getShopCarList(Model model, HttpServletRequest request)
    {
        String username = (String)request.getSession().getAttribute("username");
        if(username == null){
            model.addAttribute("message","用户不存在请登录");
            return "WEB-INF/view/login.jsp";
        }
        List<CatalogModel> list1 = catalogService.findAllCata();
        model.addAttribute("titl",list1);
        int userid = (Integer) request.getSession().getAttribute("userid");
        Logger logger=Logger.getLogger(ShoppingController.class);
        List<CartVo> list= cartService.findCartById(userid);
        System.out.println("================");
        System.out.println(list);
        if(list!=null)
        {
            model.addAttribute("list",list);
            logger.info("数据查询成功");
        }
        return "WEB-INF/view/shopping.jsp";
    }

//    跳入shopping_result.jsp
    @RequestMapping("/shopresult")
    public String forwardToShopResult(Model model)
    {
        List<CatalogModel> list1 = catalogService.findAllCata();
        model.addAttribute("titl",list1);
        return "WEB-INF/view/shop_result.jsp";
    }

    @RequestMapping("deleteOrder")
    public String deleteOrder(@RequestParam("orderId") String orderId){
        System.out.println(orderId);
        cartService.deleteOrder(orderId);
        return "/shoplist";
    }
    @RequestMapping("changeQuantity")
    public String changeQuantity(@RequestParam("orderId")String orderId,@RequestParam("quantity")String quantity){
        cartService.changeQuantity(orderId,quantity);
        return "/shoplist";
    }
}
